import Mock from 'mockjs'

Mock.mock('/api/user/login', 'post', options => {
  const { username, password } = JSON.parse(options.body)
  if (username === 'admin' && password === '123456') {
    return {
      code: 200,
      message: '登录成功',
      data: {
        token: 'abcd1234'
      }
    }
  } else {
    return {
      code: 500,
      message: '用户名或密码错误,请重新登录'
    }
  }
})

Mock.mock('/api/user/info', 'get', () => {
  const token = localStorage.getItem('token')
  if (!token || token !== 'abcd1234') {
    return {
      code: 500,
      message: 'token失效,请先登录'
    }
  } else {
    return {
      code: 200,
      message: '获取用户信息成功',
      data: {
        username: 'admin',
        avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
        roles: ['admin']
      }
    }
  }
})

